<?php
apf_require_class('Dao_DAO');

class Dao_Attendance_OrgRelation extends DAO{
    public function get_table_name(){
        return 'org_relation';
    }

    public function get_read_pdo_name(){
        return 'attendance_slave';
    }

    public function get_write_pdo_name(){
        return 'attendance_master';
    }

    public function get_table_pk(){
        return 'id';
    }

    public function _find_short_assoc($where = array(),$order="user_id asc",$limit = 0,$offset = 0) {
        apf_require_class("Dao_Attendance_UserInfo");
        $dao_userinfo = new Dao_Attendance_UserInfo();
        //月初，月末
        $year = $where['select_year'];
        $month = $where['select_month'];
        $dis_start = $year."-".$month."-01 00:00:00";//当月地一天
        $dis_end = date("Y-m-d H:i:s",mktime(23,59,59,$month+1,0,$year));//月末

        $_where = "";
        if(!empty($where['level'])) {
            $_where = " and level = {$where['level']} ";
        }
        if(!empty($where['name'])) {
            $_where .= " and (name like '{$where['name']}%' or code = '{$where['name']}')";
        }
        if(!empty($where['city_name'])) {
            $_where .= " and city_name = '{$where['city_name']}'";
        }
        if(!empty($where['department_id'])) {
            $_where .= " and department_id = {$where['department_id']}";
        }
        $_limit='';
        if($limit > 0){
            $_limit ="limit {$limit} offset {$offset}";
        }
        $_where .= " and (dimission_date='0000-00-00 00:00:00' OR (dimission_date >= '".$dis_start."' AND dimission_date < '".$dis_end."')) ";
        $s_sql = "select org.user_id,user.name,user.code,user.department_id,user.department_name,user.city_name,user.enter_date,user.dimission_date,user.user_type,user.user_type_date from {$this->get_table_name()} as org
                  left join {$dao_userinfo->get_table_name()} as user
                  on org.user_id = user.user_id
                  where org.superior_id = {$where['superior_id']}
                  {$_where}
                  order by user.enter_date asc {$_limit}";

        $rs = $this->execute($s_sql);
        $result = array();
        if(!empty($rs)) {
            foreach($rs as $key => $v) {
                $result[$v['user_id']] = $v;
            }
        }
        return $result;
    }

    public function _find_count($where = array()) {
        apf_require_class("Dao_Attendance_UserInfo");
        $dao_userinfo = new Dao_Attendance_UserInfo();

        $year = $where['select_year'];
        $month = $where['select_month'];
        $dis_start = $year."-".$month."-01 00:00:00";//当月地一天
        $dis_end = date("Y-m-d H:i:s",mktime(23,59,59,$month+1,0,$year));//月末

        $_where = "";
        if(!empty($where['level'])) {
            $_where = " and level = {$where['level']} ";
        }
        if(!empty($where['name'])) {
            $_where .= " and (name like '{$where['name']}%' or code = '{$where['name']}')";
        }
        if(!empty($where['city_name'])) {
            $_where .= " and city_name = '{$where['city_name']}'";
        }
        if(!empty($where['department_id'])) {
            $_where .= " and department_id = {$where['department_id']}";
        }
        $s_sql = "select count(*) as total_rows from {$this->get_table_name()} as org
        left join {$dao_userinfo->get_table_name()} as user
        on org.user_id = user.user_id
        where org.superior_id = {$where['superior_id']}
        {$_where}";
        $_where .= " and (dimission_date='0000-00-00 00:00:00' OR (dimission_date >= '".$dis_start."'))";

        $rs = $this->execute($s_sql);
        return $rs['0']['total_rows'];
    }

    //for hr
    public function _find_short_assoc_hr($where = array(), $order = "user_id asc", $limit = 0, $offset = 0) {
        apf_require_class ( "Dao_Attendance_UserInfo" );

        $year = $where['select_year'];
        $month = $where['select_month'];
        $dis_start = $year."-".$month."-01 00:00:00";//当月地一天
        $dis_end = date("Y-m-d H:i:s",mktime(23,59,59,$month+1,0,$year));//月末
        //var_dump($dis_start,$dis_end);
        //exit();

        $dao_userinfo = new Dao_Attendance_UserInfo ();
        $_where = " 1=1 ";
        if (! empty ( $where ['level'] )) {
            $_where = " and level = {$where['level']} ";
        }
        if (! empty ( $where ['name'] )) {
            $_where .= " and (name like '{$where['name']}%' or code = '{$where['name']}')";
        }
        if (! empty ( $where ['city_name'] )) {
            $_where .= " and city_name = '{$where['city_name']}'";
        }
        if (! empty ( $where ['department_id'] )) {
            $_where .= " and department_id = {$where['department_id']}";
        }

        $_where .= " and (dimission_date='0000-00-00 00:00:00' OR (dimission_date >= '".$dis_start."'))";
        $s_sql = "select user.user_id,user.name,user.code,user.department_id,user.department_name,user.city_name,user.enter_date,user.dimission_date,user.user_type,user.user_type_date from
        {$dao_userinfo->get_table_name()} as user
        where
        {$_where}
        order by user.enter_date asc limit {$limit} offset {$offset}";

        $rs = $this->execute ( $s_sql );
        $result = array ();
        if (! empty ( $rs )) {
            foreach ( $rs as $key => $v ) {
                $result [$v ['user_id']] = $v;
            }
        }
        return $result;
    }
    public function _find_count_hr($where = array()) {
        apf_require_class ( "Dao_Attendance_UserInfo" );
        $dao_userinfo = new Dao_Attendance_UserInfo ();
        $_where = " 1=1";

        $year = $where['select_year'];
        $month = $where['select_month'];
        $dis_start = $year."-".$month."-01 00:00:00";//当月地一天
        $dis_end = date("Y-m-d H:i:s",mktime(23,59,59,$month+1,0,$year));//月末

        if (! empty ( $where ['level'] )) {
            $_where = " and level = {$where['level']} ";
        }
        if (! empty ( $where ['name'] )) {
            $_where .= " and (name like '{$where['name']}%' or code = '{$where['name']}')";
        }
        if (! empty ( $where ['city_name'] )) {
            $_where .= " and city_name = '{$where['city_name']}'";
        }
        if (! empty ( $where ['department_id'] )) {
            $_where .= " and department_id = {$where['department_id']}";
        }
        $_where .= " and (dimission_date='0000-00-00 00:00:00' OR (dimission_date >= '".$dis_start."' AND dimission_date < '".$dis_end."'))";
        $s_sql = "select count(*) as total_rows from {$dao_userinfo->get_table_name()} as user
                        where
                        {$_where}";

        $rs = $this->execute ( $s_sql );
        return $rs ['0'] ['total_rows'];
    }


}